MVC.JFile.prototype.container = function(){
	return (this.directory() == "" ? MVC.$E('/').next() : MVC.$E(this.directory()).next());
};



/**
 * Inserts a file into a directory
 * @param {Object} directory
 * @param {Object} file_name
 * @param {Object} rename_form - if you want a form to rename the file present
 * @param {Object} set_as_selected - if you want to set the file as the selected file
 */




IDE = {};
/* ===========================================================================================
 * Utility functions
 * ==========================================================================================*/
IDE.clear_selection = function() {
	var ds = document.selection;
	if(ds && ds.empty){
		ds.empty() ;
	} else if(window.getSelection) {
		var sel=window.getSelection();
		if(sel && sel.collapse)
			sel.removeAllRanges() ;
	}
};
if(document.selection) {
    Range = function(textRange)
    {
        this.textRange = textRange;
    };
    Range.prototype.isDocumentSelected = function()
    {
        return this.textRange.offsetLeft == 2 && this.textRange.boundingLeft == 2;
    };
    
    Range.getWindowRange = function()
    {
        return new Range( document.selection.createRange() );
    };
}
else 
{
    Range.getWindowRange = function()
    {
        return window.getSelection().getRangeAt(0);
    };
    Range.prototype.isDocumentSelected = function()
    {
        var a = 1;
        return true;
    };
}




Container = function(files_dom, path){
	if(files_dom){
		IDE.recursive_clean(path, files_dom );
		return files_dom;
	}
	var files_div = document.createElement('div');
	files_div.className = 'container';
	files_div.style.display = 'none';
	return files_div;
};

IDE.recursive_clean = function(path, files_dom){
	var path_prepend = path;
	if(path_prepend!='')
		path_prepend = path_prepend+"/";
	var ch_els = files_dom.childElements();
	for(var i = 0 ; i < ch_els.length; i++)
	{
		var child_el = ch_els[i];
		if(child_el.id =="") continue;
		var f= new MVC.JFile(child_el.id);
		var old_path = child_el.id;
		
		child_el.id = path_prepend + f.file_and_extension();
		if(child_el.getAttribute('type') == 'directory')
			IDE.recursive_clean(child_el.id, child_el.next());
		else{
			editAreaLoader.getRenameFile('EDITAREA_TEXTAREA', old_path, child_el.id, f.file_and_extension());
		}
	}
};



/* ===========================================================================================
 * Opening a file / folder functionality
 * ==========================================================================================*/

IDE.upload_response = function(object) {
	MVC.$E("Upload").hide();
	DirectoryView.update(object);
};

IDE.select = function( event, element){
	if(IDE.selected && MVC.$E(IDE.selected)) { MVC.$E(IDE.selected).style.backgroundColor = ''; MVC.$E(IDE.selected).style.color = '';}
	el = (element ? MVC.$E(element): MVC.$E(this));
	el.style.backgroundColor = 'rgb(49,106,197)';
	el.style.color = 'White';
	
	IDE.selected = el.id;
		return false;
		
};




( function(){
var frame,upload,files,container,hoffset = 0, voffset=0;;

if(!window.XMLHttpRequest){ //H a c k !
	hoffset = -9;
	voffset = -2;
}
	
	
if(window.innerHeight){
	getDimensions = function(){
		return {width: window.innerWidth, height: window.innerHeight};
	};
}else{
	getDimensions = function(){
		var el = MVC.$E(document.documentElement);
		return {width: el.getWidth(), height: el.getHeight()};
	};
}


set_height = function(){
	var d = getDimensions();
	var  height = d.height, width = d.width;
	
	if(!frame){
		frame = MVC.$E('frame_EDITAREA_TEXTAREA');
		if(!frame) return;
		
		upload = MVC.$E('Upload');
		files = MVC.$E('FILES');
		container = MVC.$E('files_container')
	}
	var fs = frame.style;
	fs.width = (width-280)+'px';
	fs.height = (height-0)+'px';
	
	if(upload.style.display !='none' ){
		//MVC.$E('multipleupload').style.width = (d.width - 252)+'px';
		//MVC.$E('multipleupload').style.height = (d.height - 3)+'px';
		files.style.height = (height - 32-250)+'px';
		container.style.height = (height - 0-250)+'px';
	}else{
		files.style.height = (height - 32+voffset)+'px';
		container.style.height = (height - 0+voffset)+'px';
	}

	frame.style.width = (width-251+hoffset)+'px'; //everything but IE 6
};
})();




function stopPropagation(event)
{
    if(!event)
            event = window.event;
    try{
	
    event.cancelBubble = true;
    
    if (event.stopPropagation) 
        event.stopPropagation(); 
        
    if (event.preventDefault) 
        event.preventDefault();
	Event.stop(event);
    }catch(e)
    {}
    return false;
};


close_upload = function(){
	document.getElementById('Upload').style.display = 'none';
	set_height();
};